﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ECommon
{
    /// <summary>
    /// 字符串工具
    /// </summary>
    public static class StringTool
    {
        /// <summary>
        /// 首字母大写
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <returns>转换后的结果</returns>
        public static string FirstLetterUpper(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return source;
            }
            if (source.Length <= 1)
            {
                return source.ToUpper();
            }
            return source.First().ToString().ToUpper() + source.Substring(1);
        }

        /// <summary>
        /// 首字母小写
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <returns>转换后的结果</returns>
        public static string FirstLetterLower(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return source;
            }
            if (source.Length <= 1)
            {
                return source.ToUpper();
            }
            return source.First().ToString().ToLower() + source.Substring(1);
        }

        /// <summary>
        /// 转换字符串为帕斯卡模式
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <param name="separator">字符串分隔符</param>
        /// <returns>转换后的结果</returns>
        public static string ToPascal(string source, char separator = '_')
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return source;
            }
            List<string> parts = source.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries).ToList();
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < parts.Count; i++)
            {
                stringBuilder.Append(FirstLetterUpper(parts[i]));
            }
            return stringBuilder.ToString();
        }



        /// <summary>
        /// 转换字符串为分割符模式
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <param name="separator">字符串分隔符</param>
        /// <returns>转换后的结果</returns>
        public static string ToSeparator(string source, char separator = '_')
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return source;
            }
            List<char> chars = new List<char>();
            for (int i = 0; i < source.Length; i++)
            {
                if (_upperLetter.Contains(source[i]))
                {
                    if (i != 0)
                    {
                        chars.Add(separator);
                    }
                    chars.Add(source[i].ToString().ToLower()[0]);
                }
                else
                {
                    chars.Add(source[i]);
                }
            }
            return new string(chars.ToArray());
        }

        /// <summary>
        /// 转换字符串为驼峰模式
        /// </summary>
        /// <param name="source">原始字符串</param>
        /// <param name="separator">字符串分隔符</param>
        /// <returns>c转换后的结果</returns>
        public static string ToCamel(string source, char separator = '_')
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return source;
            }
            List<string> parts = source.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries).ToList();
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < parts.Count; i++)
            {
                if (i == 0)
                {
                    stringBuilder.Append(FirstLetterLower(parts[i]));
                }
                else
                {
                    stringBuilder.Append(FirstLetterUpper(parts[i]));
                }
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// upper letter
        /// </summary>
        private static List<char> _upperLetter = new List<char>(){ 'A', 'B', 'C', 'D', 'E', 'F', 'G',
        'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T', 'U',
        'V', 'W', 'X', 'Y', 'Z'};

        #region 正则表达式部分
        /// <summary>
        /// 正则：全数字判断
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>判断结果</returns>
        public static bool RegexMatchNumber(string origin)
        {
            Regex regex = new Regex(@"\d");
            return regex.IsMatch(origin);
        }

        /// <summary>
        /// 正则：获取全部数字
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>数字</returns>
        public static string RegexGetNumber(string origin)
        {
            return Regex.Replace(origin, @"[^\d]", "");
        }

        /// <summary>
        /// 正则：decimal判断
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>判断结果</returns>
        public static bool RegexMatchDecimal(string origin)
        {
            Regex regex = new Regex(@"^[+-]?\d*[.]?\d*$");
            return regex.IsMatch(origin);
        }

        /// <summary>
        /// 正则：获取全部decimal
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>decimal</returns>
        public static string RegexGetDecimal(string origin)
        {
            return Regex.Replace(origin, @"[^\d.+-]", "");
        }

        /// <summary>
        /// 正则：全可编辑字符判断
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>判断结果</returns>
        public static bool RegexMatchEditable(string origin)
        {
            Regex regex = new Regex(@"[ \da-zA-Z:.+/-]*");
            return regex.IsMatch(origin);
        }

        /// <summary>
        /// 正则：获取全可编辑字符
        /// </summary>
        /// <param name="origin">原始字符串</param>
        /// <returns>判断结果</returns>
        public static string RegexGetEditable(string origin)
        {
            return Regex.Replace(origin, @"[^ \da-zA-Z:/-]", "");
        }
        #endregion
    }
}
